package com.example.jdai.jdai.ui;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.jdai.jdai.MyApplication;
import com.example.jdai.jdai.R;
import com.example.jdai.jdai.rest.BackRest;
import com.example.jdai.jdai.util.DataAll;
import com.example.jdai.jdai.util.DataZfb;
import com.example.jdai.jdai.util.Tools;

import java.util.Map;

import butterknife.BindString;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnLongClick;

public class TbActivity extends Activity implements BackRest.DoLong {

    @BindView(R.id.tb)
    TopBar tb;
    @BindString(R.string.no_complete)
    String onComplete;
    ProgressDialog pd;
    @BindString(R.string.sms_tip)
    String smsTip;
    @BindView(R.id.iv_gr)
    ImageView ivGr;
    @BindView(R.id.bt_getqr)
    Button btGetqr;
    Bitmap tmp;
    @BindView(R.id.bt_cktb)
    Button btCktb;
    @BindView(R.id.ll_ck)
    LinearLayout llCk;
    @BindView(R.id.activity_bt)
    LinearLayout activityBt;
    @BindView(R.id.tv_ewm)
    TextView tvEwm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tb);
        ButterKnife.bind(this);
        pd = new ProgressDialog(this);
        pd.setCanceledOnTouchOutside(false);
        pd.setMessage(smsTip);
        tb.setTopBarLisener(new TopBar.TopBarLisener() {
            @Override
            public void back() {
                finish();
                saveAll();
            }

            @Override
            public void more() {
                if (!TextUtils.isEmpty(MyApplication.dataAll.dataZfb.data.get(DataZfb.ZFB_TOKEN))) {
                    pd.setMessage(smsTip);
                    pd.show();
                    MyApplication.backRest.TbCk(TbActivity.this);
                } else {
                    Toast.makeText(TbActivity.this, "请先获取二维码", Toast.LENGTH_SHORT).show();
                }


            }
        });
        if (savedInstanceState == null) {
            initData();
        }

    }

    void initData() {
        MyApplication.dataAll.dataZfb.data.put(DataZfb.ZFB_TOKEN, "");
        MyApplication.dataAll.dataZfb.saveAll();

    }

    boolean saveAll() {
//        DataAll.dataZfb.data.put(DataZfb.ZFB_NAME,etName.getText().toString().trim());
//        DataAll.dataZfb.data.put(DataZfb.ZFB_PWD,etPwd.getText().toString().trim());
//        return DataAll.dataZfb.saveAll();
        return true;
    }

    @Override
    protected void onDestroy() {
        saveAll();
        finish();
        super.onDestroy();

    }

    @Override
    protected void onPause() {
        super.onPause();
        saveAll();
    }
//    //ok支付宝
//    public  void ZfbOk(final  Activity activity){
//        SendCode send=new SendCode ();
//        send.data.put("user_id", MyApplication.dataAll.data.get(DataAll.LOGIN_ID));
//        send.data.put("zfb_username", DataAll.dataZfb.data.get(DataZfb.ZFB_NAME));
//        send.data.put("zfb_pwd", DataAll.dataZfb.data.get(DataZfb.ZFB_PWD));
//        Call call =MyApplication.okClient.newCall(Tools.getRequest(Config.ZFB_UP_URL,send));
//        call.enqueue(new Callback()
//        {
//            @Override
//            public void onFailure(Call call, IOException e) {
//
//            }
//            @Override
//            public void onResponse(Call call, Response response) throws IOException {
//                int code=response.code();
//                if(200==code){
//                    if (Config.ISDEBUG)
//                        Log.v("zfb",response.body().string());
//                }
//                else if (201==code){
//                    Tools.postToMain(new Runnable() {
//                        @Override
//                        public void run() {
//                            Toast.makeText(MyApplication.applicationContext,"需要验证码",Toast.LENGTH_SHORT).show();
//                            Intent intent=new Intent();
//                            intent.putExtra("plat",1);
//                            startActivity(new Intent(ZfbActivity.this,YzmActivity.class));
//                        }
//                    });
//
//                }
//
//            }
//
//        });
//    }

    @Override
    public void doDone(int mark, Map str) {
        switch (mark) {
            case 1:
                Toast.makeText(MyApplication.applicationContext, "支付宝需要验证码", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(TbActivity.this, YzmActivity.class);
                intent.putExtra("plat", 1);
                intent.putExtra("token", MyApplication.dataAll.data.get(DataAll.TMP));
                startActivity(intent);
                pd.dismiss();
                finish();
                break;
            case 200:
                pd.dismiss();
                byte[] byteIcon = Base64.decode((String) str.get("qr_img"), Base64.DEFAULT);
                MyApplication.dataAll.dataZfb.data.put(DataZfb.ZFB_TOKEN, (String) str.get("token"));
                MyApplication.dataAll.dataZfb.saveAll();
                Bitmap bitmap = BitmapFactory.decodeByteArray(byteIcon, 0, byteIcon.length);
                tmp = Bitmap.createScaledBitmap(bitmap, ivGr.getWidth(), ivGr.getHeight(), false);
                ivGr.setImageBitmap(tmp);
                llCk.setVisibility(View.VISIBLE);
                tvEwm.setText("已刷新二维码");
                break;
            case 2:
                pd.dismiss();
                Toast.makeText(MyApplication.applicationContext, "密码或账号错误", Toast.LENGTH_SHORT).show();

                break;
            case 666:
                pd.dismiss();
                MyApplication.dataAll.dataZfb.data.put(DataZfb.ZFB_STATUS, DataAll.OK);
                MyApplication.dataAll.dataZfb.saveAll();
                MyApplication.startNext(TbActivity.this);
                finish();
                break;
            case 999:
                pd.dismiss();
                tvEwm.setText("请刷新二维码");
                Toast.makeText(MyApplication.applicationContext, "淘宝未验证成功", Toast.LENGTH_SHORT).show();
                break;
            case 400:
                pd.dismiss();
                tvEwm.setText("请刷新二维码");
                Toast.makeText(MyApplication.applicationContext, "淘宝未验证成功", Toast.LENGTH_SHORT).show();
                break;

        }
    }

    @OnLongClick(R.id.iv_gr)
    public boolean onLongClick() {
        if (tmp != null) {
            try {
                Tools.updataMedia(this, Tools.saveMyBitmap("二维码", tmp));
            } catch (Exception e) {
                Log.v("error", e.getMessage());
            }


        }

        Toast.makeText(TbActivity.this, "二维码已保存到手机根目录", Toast.LENGTH_SHORT).show();

        return true;

    }

    @OnClick(R.id.bt_getqr)
    public void onClick() {
        pd.setMessage("获取二维码中...");
        pd.show();
        btGetqr.setText("刷新二维码");
        MyApplication.backRest.TbGetQr(TbActivity.this);

    }

    @OnClick(R.id.bt_cktb)
    public void onCkClick() {
        if (!TextUtils.isEmpty(MyApplication.dataAll.dataZfb.data.get(DataZfb.ZFB_TOKEN))) {
            pd.setMessage(smsTip);
            pd.show();
            MyApplication.backRest.TbCk(TbActivity.this);
        } else {
            Toast.makeText(TbActivity.this, "你未扫描二维码", Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }
}
